//
//  HQLoginViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/5.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQLoginViewController.h"
#import "AppDelegate.h"

@interface HQLoginViewController ()

@end

@implementation HQLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _phoneNo.text = [UserModelManager phone];
    [self setPlaceHolderColor:_phoneNo];
    [self setPlaceHolderColor:_password];
    [self addKeyBoardObserver];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    CGRect frame = self.view.frame;
    frame.origin.y = -60;
    self.view.frame = frame;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    self.view.frame = frame;
}

/**
 注册账号

 @param sender 注册
 */
- (IBAction)registerItem:(id)sender {
    HQRegisterViewController *registerVc = [[HQRegisterViewController alloc]init];
    [self presentViewController:registerVc animated:YES completion:^{
        
    }];
}

/**
 忘记密码
 
 @param sender 忘记密码
 */
- (IBAction)forgetPasswordItem:(id)sender {
    HQForgetPasswordViewController *forgetVc = [[HQForgetPasswordViewController alloc]init];
    [self presentViewController:forgetVc animated:YES completion:^{
        
    }];
}

/**
 登录账号

 @param sender 登录
 */
- (IBAction)loginItem:(id)sender {
    if([self isInputEmpty:_phoneNo.text]){
        [self tostView:@"请输入手机号"];
        return;
    }
    if(![HQTool phoneNoRegex:_phoneNo.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }
    if([self isInputEmpty:_password.text]){
        [self tostView:@"请输入密码"];
        return;
    }
    [self showHudText:@"登录中..."];
    [HQNetWork requetParam:[HQParam loginParamWithUserName:_phoneNo.text withPassword:_password.text] withMethod:[HQMethod login] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            UserModel *userModel = [[UserModel alloc]init];
            userModel.username = sucData[@"data"][@"real_name"];
            userModel.userId = sucData[@"data"][@"id"];
            userModel.phone = sucData[@"data"][@"phone"];
            userModel.shopName = sucData[@"data"][@"shop_name"];
            userModel.portrait = sucData[@"data"][@"portrait"];
            userModel.position = sucData[@"data"][@"position"];
            [UserModelManager saveUserModel:userModel withLoginState:YES];
            [(AppDelegate *)[UIApplication sharedApplication].delegate hiddenLogin];
        }else{
//            [self.view makeToast:@"拨打客服电话 010 8622 0305 申请开通" duration:5.0f position:@"bottom"];
            [self.view makeToast:sucData[kInfo] duration:5.0f position:@"bottom"];
        }
    } fail:^{
        [self hiddnHud];
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"蜂窝移动数据已关闭" message:@"打开蜂窝移动数据或使用无线局域网来访问数据"  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
   
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"设置" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication]canOpenURL:url]) {
                [[UIApplication sharedApplication]openURL:url];
            }
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }];
}

@end
